//
//  NSData+AYExtension.h
//  AYink
//
//  Created by michael on 2024/9/2.
//

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSData (AYExtension)

@property (nonatomic, assign, readonly) int8_t ay_int8;
@property (nonatomic, assign, readonly) uint8_t ay_uint8;
@property (nonatomic, assign, readonly) int16_t ay_int16;
@property (nonatomic, assign, readonly) uint16_t ay_uint16;
@property (nonatomic, assign, readonly) int32_t ay_int32;
@property (nonatomic, assign, readonly) uint32_t ay_uint32;
@property (nonatomic, assign, readonly) int64_t ay_int64;
@property (nonatomic, assign, readonly) uint64_t ay_uint64;
@property (nonatomic, copy, readonly, nullable) NSString *ay_hex;

- (int8_t)ay_int8At:(NSUInteger)idx;
- (uint8_t)ay_uint8At:(NSUInteger)idx;
- (int16_t)ay_int16At:(NSUInteger)idx;
- (uint16_t)ay_uint16At:(NSUInteger)idx;
- (int32_t)ay_int32At:(NSUInteger)idx;
- (uint32_t)ay_uint32At:(NSUInteger)idx;
- (int64_t)ay_int64At:(NSUInteger)idx;
- (uint64_t)ay_uint64At:(NSUInteger)idx;

- (NSData *)ay_aes128EncryptWithKey:(NSData *)key;
- (NSData *)ay_aes128DecryptWithKey:(NSData *)key;

@end
@interface NSString (AYExtension)

@property (nonatomic, copy, readonly, nullable) NSData *ay_hexData;

@end

NS_INLINE NSData *AYDataFromLen(NSData *data, NSUInteger from, NSUInteger length) {
    return [data subdataWithRange:NSMakeRange(from, length)];
}
NS_INLINE NSData *AYInt8Data(int8_t value) {
    return [NSData dataWithBytes:&value length:sizeof(value)];
}
NS_INLINE NSData *AYUInt8Data(uint8_t value) {
    return [NSData dataWithBytes:&value length:sizeof(value)];
}
NS_INLINE NSData *AYInt16Data(int16_t value) {
    return [NSData dataWithBytes:&value length:sizeof(value)];
}
NS_INLINE NSData *AYUInt16Data(uint16_t value) {
    return [NSData dataWithBytes:&value length:sizeof(value)];
}
NS_INLINE NSData *AYInt32Data(int32_t value) {
    return [NSData dataWithBytes:&value length:sizeof(value)];
}
NS_INLINE NSData *AYUInt32Data(uint32_t value) {
    return [NSData dataWithBytes:&value length:sizeof(value)];
}

NS_INLINE NSData * AYRemoveData(NSMutableData *data, NSUInteger length) {
    NSData *d = [data subdataWithRange:NSMakeRange(0, length)];
    [data replaceBytesInRange:NSMakeRange(0, length) withBytes:NULL length:0];
    return d;
}

NS_INLINE NSData * AYDataArray(NSArray<NSData *> *arr) {
    NSMutableData *data = [NSMutableData data];
    [arr enumerateObjectsUsingBlock:^(NSData * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [data appendData:obj];
    }];
    return [data copy];
}


NS_ASSUME_NONNULL_END
